package cn.com.xf.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.xf.common.AbstractBaseController;
import cn.com.xf.common.Constants;
import cn.com.xf.dao.TbempinfoDAO;
import cn.com.xf.dao.TsUserDAO;
import cn.com.xf.domain.Tbempinfo;
import cn.com.xf.domain.TsUser;
import cn.com.xf.service.AnalyzeService;
import cn.com.xf.service.KqfxService;
import cn.com.xf.task.AnalyzeCallable;
import cn.com.xf.task.AnalyzeTask;
import cn.com.xf.task.PrintTask;
import cn.com.xf.utils.DateUtils;
import cn.com.xf.web.form.AnalyzeForm;

@Controller
@RequestMapping("/check")
public class AnalyzeController extends AbstractBaseController {

	@Autowired
	private AnalyzeService threadAnalyzeService;
	
	@Autowired
	private AnalyzeService analyzeService;
	@Autowired
	private ThreadPoolTaskExecutor taskExecutor;
	
	@Autowired
	private KqfxService kqfxService;
	
	@Autowired
	private TbempinfoDAO tbEmpinfoDao;

	@RequestMapping("/analyze")
	public ModelAndView analyze(HttpServletRequest request, HttpServletResponse response, AnalyzeForm form)
			throws Exception {
		form.setSiteid(getDefaultSiteid());
		ModelAndView mv = new ModelAndView("/check/analyze", Constants.FORM_COMMAND_KEY, form);
		mv.addObject("defaultSiteid", getDefaultSiteid());
		return mv;
	}
	
	@RequestMapping("/doAnalyze")
	@ResponseBody
	public Map<String, Object> doAnalyze(HttpServletRequest request, HttpServletResponse response, @Valid AnalyzeForm form,
			BindingResult result) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			String errorJson = errorToJson(errorList);
			map.put("errorJson", errorJson);
			return map;
		}
		if(!isSiteId(form.getSiteid())) {
			FieldError e = new FieldError("", "siteid", "所属公司不合法");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			map.put("errorJson", errorJson);
			return map;
		}
		Date startDate = DateUtils.parseDate(form.getStartdate());
		Date endDate = DateUtils.parseDate(form.getEnddate());
//		threadAnalyzeService.analyze(form.getSiteid(), form.getDeptid(), form.getUserid(), startDate, endDate);
//		for(;;){
//			int count = taskExecutor.getActiveCount();
//			if (count == 0)
//				break;
//		}
		String[] userids = new String[]{};
		if (!form.getUserid().equals("")) {
			userids = form.getUserid().split(",");
		} else if (!form.getSiteid().equals("")) {
			List<Tbempinfo> list = tbEmpinfoDao.selectEmpBySiteid(form.getSiteid());
			userids = new String[list.size()];
			for (int i = 0; i < list.size(); i++) {
				userids[i] = list.get(i).getEmpid();
			}
		}
		
//		Future<Integer> task = kqfxService.analyze(userids, startDate, endDate);
//		int retcode = -1;
//		try {
//			retcode = task.get();
//		} catch (Exception e) {
//			retcode = -1;
//		}
		int retcode = kqfxService.analyze(userids, startDate, endDate);
		if (retcode == 1) {
			
			map.put("status", "SUCCESS");
		}
		return map;
	}

}
